$(function() {
 
	// 回车按键 查询
	document.onkeydown = function(event) {
		var e = event ? event : (window.event ? window.event : null);
		if (e.keyCode == 13) {
			var hasDlg = false;
			$(".window").each(
					function() {
						if ($(this).css('display') != "none"
								&& $(this).hasClass("panel")) {
							hasDlg = true;
						}
					});
			if (!hasDlg) {
				queryequipmentInfo();
			} else {

			}
		}
	}
	queryequipmentInfoList();
});
// 查询器材信息
function queryequipmentInfoList() {

	datagrid = $("#equipmentInfo_table_2").datagrid(
			{
				url : base + '/manage/queryEquipmentInfoList.action',
				queryParams : {},
				loadMsg : '数据加载中...',
				collapsible : true,
				nowrap : false,
				striped : true,
				fitColumns : true,
				rownumbers : true,
				singleSelect : true,
				selectOnCheck : true,
				checkOnSelect : true,
				remoteSort : false,
				pagination : true,
				pageSize : 20,//分页大小
				pageList : [ 10, 20, 50, 100 ],//每页的个数
				fit : true,//自动补全
				hideColumn : [ [ {
					field : 'eId',
					resizable : false
				} ] ],
				columns : [ [ {
					field : 'eName',
					title : '名称',
					width : 80,
				}, {
					field : 'ePrice',
					title : '价格',
					width : 60,
				}, {
					field : 'useState',
					title : '使用状态',
					width : 80,
					formatter:function(value,row,index){
						if(value=="111"){
							return "空闲";
						}else if(value=="222"){
							return "预定中";
						}else if(value=="333"){
							return "使用中";
						}else{
							return "异常";
						}
					}
				}, {
					field : 'eBuyTime',
					title : '购买时间',
					width : 80
				}, {
					field : 'eIsOk',
					title : '硬件状态',
					width : 60,
					formatter:function(value,row,index){
						if(value=="T"){
							return "正常";
						}else{
							return "损坏";
						}
					}
				}, {
					field : 'eType',
					title : '类型',
					width : 60,
					formatter:function(value,row,index){
						if(value=="111"){
							return "跑步机";
						}else if(value=="222"){
							return "划船器";
						}else if(value=="333"){
							return "肌肉组";
						}else if(value=="444"){
							return "仰卧器";
						}else if(value=="555"){
							return "健步车";
						}else if(value=="666"){
							return "美腰机";
						}else if(value=="777"){
							return "登梯器";
						}else if(value=="888"){
							return "引体杆";
						}else if(value=="999"){
							return "瑜伽球";
						}else{
							return "异常";
						}
					}
				}, {
					field : 'eArea',
					title : '区域',
					width : 60
				}, {
					field : 'lastModifiedBy',
					title : '最后修改人',
					width : 100
				}, {
					field : 'lastModifiedTime',
					title : '最后修改时间',
					width : 100
				}, {
					field : 'eNeedLevel',
					title : '预定所需等级',
					width : 60
				}, {
					field : 'eNeedExp',
					title : '预定所需经验',
					width : 60
				}, {
					field : 'eIsValid',
					title : '是否有效',
					width : 60,
					formatter:function(value,row,index){
						if(value=="T"){
							return "启用";
						}else{
							return "作废";
						}
					}
				} ] ],

				// 显示html语言
				loadFilter : function(data) {
					for (var i = 0; i < data.rows.length; i++) {
						for ( var att in data.rows[i]) {
							if (typeof (data.rows[i][att]) == "string") {
								data.rows[i][att] = data.rows[i][att].replace(
										/</g, "&lt;").replace(/>/g, "&gt;");
							}
						}
					}
					return data;
				},
				// 加载
				onLoadSuccess : function(data) {
					$(this).datagrid("fixRownumber"); // 序号自适应
					
					//重新渲染tooltip
					$(".easyui-panel").tooltip({
						position: 'bottom',
						hideEvent: 'none',
						onShow: function(){
							$(this).tooltip('tip').css({
								backgroundColor: '##464646',
								borderColor: '#ff0000',
								boxShadow: '1px 1px 3px #292929'
							});
							var t = $(this);
							t.tooltip('tip').focus().unbind().bind('blur',function(){
								t.tooltip('destroy');
							});
						},
						onPosition: function(){
							$(this).tooltip('tip').css('left', $(this).offset().left);
							$(this).tooltip('arrow').css('left', 20);
						}
					});

				}
			});
}


//修改页面初始化
function editequipmentInfo() {
	if (null == $('#equipmentInfo_table_2').datagrid('getRows')) {
		$.messager.alert('提示信息', '请先注册一条器材信息信息!', 'info');
		return false;
	}
	var rows = $('#equipmentInfo_table_2').datagrid('getSelections');
	if (rows.length <= 0) {
		$.messager.alert('提示信息', '请先选择一条器材信息!', 'info');
	} else {
		var i = rows.length - 1;
		var eId = rows[i].eId;
		$.ajax({
			url : base + "/manage/queryEquipmentInfo.action",
			type : "post",
			data : {
				eId : eId
			},
			cache : false,
			success : function(data) {
				if (data != null) {
					$("#equipmentInfoEdit_form").form('clear');
					$('#equipmentInfoEditDiv').window("open");
					$('#saveequipmentInfoForm1').show();
					$("#edit_eIsOk").combobox('setValue', data.eIsOk);
					$("#edit_eId").val(data.eId);
					$("#edit_eArea").textbox('setValue', data.eArea);
					$("#edit_eNeedLevel").textbox('setValue', data.eNeedLevel);
					$("#edit_eNeedExp").textbox('setValue', data.eNeedExp);
				} else {
					$.messager.alert("提示信息", "失败，该条数据不存在！", "info");
					$("#equipmentInfo_table_2").datagrid('reload');
				}
			}
		});
	}
}
//新增页面初始化
function addequipmentInfo() {
	$("#equipmentInfoAdd_form").form('clear');
	$('#equipmentInfoAddDiv').window("open");
	$('#saveequipmentInfoForm2').show();
}
// toolbar 启用停用
function openOrCloseequipmentInfo() {
	if (null == $('#equipmentInfo_table_2').datagrid('getRows')) {
		$.messager.alert('提示信息', '请先注册一条器材信息!', 'info');
		return false;
	}
	var rows = $('#equipmentInfo_table_2').datagrid('getSelections');
	if (rows.length <= 0) {
		$.messager.alert('提示信息', '请先选择一条器材信息!', 'info');
	} else {
		var i = rows.length - 1;
		var id = rows[i].eId;
		var valid = rows[i].eIsValid;
		if (valid == "T") {
			valid = 'F';
			var MsgCloseOrOpen = "确定停用吗？";
			var MsgCloseOrOpenisOK = "停用成功";
		} else {
			valid = 'T';
			var MsgCloseOrOpen = "确定启用吗？";
			var MsgCloseOrOpenisOK = "启用成功";	
		}
		$.messager
				.confirm(
						'提示信息',MsgCloseOrOpen,function(data) {
							if (data) {
								$.ajax({
										url : base+ "/manage/saveEquipmentInfo.action",
										type : "post",
										data : {
											eId : id,
											eIsValid : valid
										},
										cache : false,
										success : function(data) {
											if (!data.success) {
												$.messager.alert("提示信息",data.message,"info");
											} else {
												$.messager.alert("提示信息",MsgCloseOrOpenisOK,"success");
												$("#equipmentInfo_table_2").datagrid('reload');
											}
												$.messager.progress('close');
											},
											async : false
								  });
							}
						});
	}
}

// 立即查询(条件查询)
function queryequipmentInfo() {
	var eName = $("#eName").val().replace(/(^\s*)|(\s*$)/g, "");
	var useState = $('#useState').combobox('getValue');
	var eIsOk = $('#eIsOk').combobox('getValue');
	var param = {
		"eName" : eName,
		"useState" : useState,
		"eIsOk" : eIsOk
	};
	$("#equipmentInfo_table_2").datagrid("load", param);

}
//清空
function clearInfo(){
	$("#eName").textbox('setValue', "");
	$('#useState').combobox('setValue',"");
	$('#eIsOk').combobox('setValue',"");
}

// 保存-修改
function submitequipmentInfoForm1() {
	// 检查修改框是否打开
	var isValidEdit = $('#equipmentInfoEdit_form').form('validate');
	if (!isValidEdit) {
		return false;
	}
	// 修改
	var edit_eIsOk = $("#edit_eIsOk").combobox("getValue");
	var edit_eArea = $("#edit_eArea").val().replace(/(^\s*)|(\s*$)/g, "");
	var edit_eNeedLevel = $("#edit_eNeedLevel").val().replace(/(^\s*)|(\s*$)/g,"");
	var edit_eNeedExp = $("#edit_eNeedExp").val().replace(/(^\s*)|(\s*$)/g,"");
	var edit_eId = $("#edit_eId").val().replace(/(^\s*)|(\s*$)/g, "");
	// 防呆
	if (edit_eArea == "" || edit_eArea == null) {
		$.messager.alert("提示信息", "区域不能为空！", "info");
		return false;
	}
	if (edit_eNeedLevel == "" || edit_eNeedLevel == null) {
		$.messager.alert("提示信息", "所需等级不能为空！", "info");
		return false;
	}
	if (edit_eNeedExp == "" || edit_eNeedExp == null) {
		$.messager.alert("提示信息", "所需经验不能为空！", "info");
		return false;
	}
	$.ajax({
		url : base + "/manage/saveEquipmentInfo.action",
		type : "post",
		data : {
			"eIsOk":edit_eIsOk,
			"eArea":edit_eArea,
			"eNeedLevel":edit_eNeedLevel,
			"eNeedExp":edit_eNeedExp,
			"eId":edit_eId
		},
		cache : false,
		success : function(data) {
			if (!data.success) {
				$.messager.alert("提示信息", data.message, "info");
			} else {
				$.messager.alert("提示信息", "修改成功！", "success");
				$("#equipmentInfo_table_2").datagrid('reload');
				closeequipmentInfoForm();
			}
			$.messager.progress('close');
		},
		async : false
	});
}

// 保存-新增
function submitequipmentInfoForm2() {
	// 检查新增框是否打开
	var isValidEdit = $('#equipmentInfoAdd_form').form('validate');
	if (!isValidEdit) {
		return false;
	}
	// 新增
	var add_eName = $("#add_eName").val().replace(/(^\s*)|(\s*$)/g, "");
	var add_ePrice = $("#add_ePrice").val().replace(/(^\s*)|(\s*$)/g, "");
	var add_eType = $("#add_eType").combobox("getValue");
	var add_eArea = $("#add_eArea").val().replace(/(^\s*)|(\s*$)/g, "");
	var add_eNeedLevel = $("#add_eNeedLevel").val().replace(/(^\s*)|(\s*$)/g,"");
	var add_eNeedExp = $("#add_eNeedExp").val().replace(/(^\s*)|(\s*$)/g,"");
	// 防呆
	if (add_eType == "" || add_eType == null) {
		$.messager.alert("提示信息", "请选择类型！", "info");
		return false;
	}
	if (add_eName == "" || add_eName == null) {
		$.messager.alert("提示信息", "名称不能为空！", "info");
		return false;
	}
	if (add_ePrice == "" || add_ePrice == null) {
		$.messager.alert("提示信息", "价格不能为空！", "info");
		return false;
	}
	if (add_eArea == "" || add_eArea == null) {
		$.messager.alert("提示信息", "区域不能为空！", "info");
		return false;
	}
	if (add_eNeedLevel == "" || add_eNeedLevel == null) {
		$.messager.alert("提示信息", "所需等级不能为空！", "info");
		return false;
	}
	if (add_eNeedExp == "" || add_eNeedExp == null) {
		$.messager.alert("提示信息", "所需经验不能为空！", "info");
		return false;
	}
	$.ajax({
		url : base + "/manage/insertEquipmentInfo.action",
		type : "post",
		data : {
			"eName":add_eName,
			"ePrice":add_ePrice,
			"eArea":add_eArea,
			"eNeedLevel":add_eNeedLevel,
			"eType":add_eType,
			"eNeedExp":add_eNeedExp
		},
		cache : false,
		success : function(data) {
			if (!data.success) {
				$.messager.alert("提示信息", data.message, "info");
			} else {
				$.messager.alert("提示信息", "新增成功！", "success");
				$("#equipmentInfo_table_2").datagrid('reload');
				closeequipmentInfoForm();
			}
			$.messager.progress('close');
		},
		async : false
	});
}
// 关闭保存页面
function closeequipmentInfoForm() {
	$('#equipmentInfoEditDiv').window('close');
	$("#equipmentInfoEdit_form").form('clear');
	$('#equipmentInfoAddDiv').window('close');
	$("#equipmentInfoAdd_form").form('clear');
}
